//
//  ScopeOfApplicationViewController.m
//  tuanGG
//
//  Created by 李洪旭 on 15/11/19.
//  Copyright (c) 2015年 weita. All rights reserved.
//

#import "ScopeOfApplicationViewController.h"
#import "LqkinDataPicker.h"
#import "ScopeTableViewCell.h"
@interface ScopeOfApplicationViewController ()<UITableViewDataSource,UITableViewDelegate>;
@property (nonatomic,strong)NSArray *dataArray;
@property (nonatomic,assign)NSInteger index;
@property (nonatomic,strong)NSMutableArray *flagArray;
@property (nonatomic,strong)UITableView *tableView;
@end

@implementation ScopeOfApplicationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:@"f0efed"];
    [self addnavigationTitle:@"适用范围" navColor:@"4fd2be"];
    [self initlizeDataSource];
    [self addTableView];
}
- (void)initlizeDataSource {
    _index = 10;
    _flagArray = [[NSMutableArray alloc]init];
    _dataArray = @[@"全场通用",@"仅适用于菜品",@"仅适用于菜品、锅底",@"除酒水饮料外全场通用",@"除酒水饮料、包间费用外全场通用",@"仅适用于80元及以下菜品代金劵+20元及以下鲜榨果汁代金劵"];
    if (![[User_member instance].applicable isEqualToString:@""]) {
        _index = [[User_member instance].applicable integerValue];
        for (int i = 0; i < 6; i ++) {
            if (_index == i ) {

                [_flagArray addObject:@(1)];
            }
            else
            {
                [_flagArray addObject:@(0)];
            }
        }

        [_tableView reloadData];
    }else
    {
        for (int i = 0; i < 6; i ++) {

            [_flagArray addObject:@(0)];

        }
        
    }
}
- (void)addTableView {
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 300)];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.scrollEnabled = NO;
    tableView.tableFooterView = [[UITableView alloc]init];
    [tableView registerClass:[ScopeTableViewCell class] forCellReuseIdentifier:@"CELL"];
    [self.view addSubview:tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ScopeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
    cell.label.text = _dataArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    BOOL isopen = [_flagArray[indexPath.row] boolValue];
    if (isopen) {
        [cell.seledImage sd_setImageWithURL:nil placeholderImage:IMGNAME(@"ic_checked")];
    }
    else
    {
        [cell.seledImage sd_setImageWithURL:nil placeholderImage:IMGNAME(@"ic_unchecked")];
    }
//    [cell.seledImage sd_setImageWithURL:nil placeholderImage:IMGNAME(@"ic_unchecked")];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    _index = indexPath.row;
    NSLog(@"%ld",_index);
    [self addAlert:[NSString stringWithFormat:@"您选择了:%@",_dataArray[_index]] otherButtonTitles:@"取消"];
    for (int i = 0; i < _flagArray.count; i ++) {
        if(i == indexPath.row){
            _flagArray[i]=@1;
        }else{

            _flagArray[i]=@0;
        }
    }
    [tableView reloadData];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        [self NetWorking];
    }
}
- (void)NetWorking {
    NSDictionary *dictionary = @{@"content":[NSString stringWithFormat:@"%ld",_index]};
    [GLAFRequest startRequest:applicable_url paramsDic:dictionary defaultdialog:YES ispost:YES handleSuccess:^(id sta, NSDictionary *paramsDic) {
        NSLog(@"%@",sta);
        if ([sta[@"code"]integerValue ] == 1) {
            [self.navigationController popViewControllerAnimated:YES];
        }
    } doFinally:^(NSDictionary *paramsDic) {
        
    }];
}

@end
